<?php
require_once ('comm/db.inc.php');

/**
 * 数据库操作类
 * Enter description here ...
 * @author fancy
 *
 */
class Book extends DBSQL {
	
	public $_BOOK = "book_info";
	public $_CATEGORY = "book_cate";
	public $_CERROR  ="book_correctError";
	public $_LWORD  = "book_leaveword";
	public $_LINK  = "book_links";
	public $_CNUM  = "book_click_num";
	public $_IPNUM  = "book_click_ip";
	public $_pagesize = 20;
	public $_home_pagesize = 8;//首页显现条数
	public $_index_listSize = 40;
	
	public function __construct(){
		parent::__construct();
	}
	
	
	/**
	 * 查询最新入库的  首页显示
	 * Enter description here ...
	 */
	public function getnewInBook(){
		$sql = "SELECT * FROM ".$this->_BOOK." order by createTime desc";
		$sql .=" LIMIT 0 , $this->_index_listSize";
		return $this->select($sql);
	}
	
	/**
	 * 查询热门书籍
	 * Enter description here ...
	 */
	public function gethotBook(){
		$sql = "SELECT * FROM ".$this->_BOOK." order by  pointNum desc";
		$sql .=" LIMIT 0 , $this->_home_pagesize";
		return $this->select($sql);
	}
	
	/**
	 * 查询好书
	 * Enter description here ...
	 */
	public function  getloveBook(){
		$sql = "SELECT * FROM ".$this->_BOOK." order by  collect desc";
		$sql .=" LIMIT 0 , $this->_home_pagesize";
		return $this->select($sql);
	}
	
	/**
	 * 查询分类
	 * Enter description here ...
	 */
	public function  getCateList(){
		$sql = "SELECT * FROM ".$this->_CATEGORY;
		return $this->select($sql);
	}
	
	/**
	 * 查询留言
	 * Enter description here ...
	 */
	public function  getleaveWord(){
		$sql = "SELECT * FROM ".$this->_LWORD." order by  createTime desc";
		$sql .=" LIMIT 0 , $this->_home_pagesize";
		return $this->select($sql);
	}
	
	/**
	 * 查询勘误信息
	 * Enter description here ...
	 */
	public function getCError(){
		$sql = "SELECT * FROM ".$this->_CERROR." order by  createTime desc";
		$sql .=" LIMIT 0 , $this->_home_pagesize";
		return $this->select($sql);
	}
	
	
	/**
	 * 根据分类分页查询
	 * Enter description here ...
	 * @param unknown_type $cateId
	 * @param unknown_type $pages
	 */
	public function seachbookByCate($cateId=0,$s="",$pages=1){
		$start = ($pages-1)*$this->_pagesize;
		if ($cateId==0){
			if($s==""){
				$sql = "SELECT * FROM ".$this->_BOOK;
			}else {
				$sql = "SELECT * FROM ".$this->_BOOK." where bookName like '%".$s."%' or author like '%".$s."%' ";
			}
			
		}else {
			if($s==""){
				$sql = "SELECT * FROM ".$this->_BOOK."  where category = $cateId";
			}else{
				$sql = "SELECT * FROM ".$this->_BOOK."  where category = $cateId and (bookName like '%".$s."%' or author like '%".$s."%') ";
			}
			
		}
		$sql .=" order by  createTime desc";
		$sql .=" LIMIT $start , $this->_pagesize";
		return $this->select($sql);
	}
	/**
	 * 根据分类查询总数
	 * Enter description here ...
	 * @param unknown_type $cateId
	 */
	public function seachbookByCateTotal($cateId=0,$s=""){
		$sql = "SELECT COUNT(BID) FROM ".$this->_BOOK."  where category = $cateId";
		if ($cateId==0){
			if($s==""){
				$sql = "SELECT COUNT(BID) FROM ".$this->_BOOK;
			}else{
				$sql = "SELECT COUNT(BID) FROM ".$this->_BOOK." where bookName like '%".$s."%' or author like '%".$s."%' ";
			}
			
		}else {
			if($s==""){
				$sql = "SELECT COUNT(BID) FROM ".$this->_BOOK."  where category = $cateId";
			}else {
				$sql = "SELECT COUNT(BID) FROM ".$this->_BOOK."  where category = $cateId and (bookName like '%".$s."%' or author like '%".$s."%') ";
			}
			
		}
		$r = $this->select($sql);
		return $r[0][0];
	}
	
	/**
	 * 根据id获取书籍
	 * Enter description here ...
	 * @param unknown_type $Bid
	 */
	public function getBook($Bid){
		$sql = "SELECT * FROM ".$this->_BOOK."  where BID = $Bid";
		return $this->select($sql);
	}
	
	/**
	 * 根据id查询表
	 * Enter description here ...
	 * @param unknown_type $id 主键id
	 * @param unknown_type $name 表名称
	 */
	public function getInfo($id,$name){
		$sql = "SELECT * FROM ".$name." WHERE BID = ".$id;
		return $this->select($sql);
	}
	
	/**
	 * 查询是否有该本书的点击量记录
	 * 单挑的记录
	 * Enter description here ...
	 * @param unknown_type $id
	 */
	public function getCNUMbyBookId($id){
		$sql = "SELECT * FROM $this->_CNUM WHERE BOOK_ID = ".$id;
		return $this->select($sql);
	}
	
	/**
	 * 查询是否有该本书的点击者的客户端ip
	 * 多条记录
	 * Enter description here ...
	 * @param unknown_type $id
	 */
	public function getIPNUMbyBookId($id){
		$sql = "SELECT * FROM $this->_IPNUM WHERE BOOK_ID = ".$id;
		return $this->select($sql);
	}
	
/**
	 * 新增数据
	 * Enter description here ...
	 * @param unknown_type $name 表名称
	 * @param unknown_type $data key表字段  val值     数组
	 */
	public function insertData($name,$data){
		$field = implode(',', array_keys($data));
		$int = 0;
		foreach ($data as $key=>$val){
			$value .= "'".$val."'";
			if($int<count($data)-1){
				$value .=",";
			}
			$int++;
		}
		
		$sql = "INSERT INTO ".$name."(".$field.") values (".$value.")";
		error_log("insert-error-msg :".$sql."\n",3,ROOT_PATH."/tmp/php_sql_err.log");
		return $this->insert($sql);
	}
	
	
/**
	 * 更新指定表指定ID的调查表记录
	 * Enter description here ...
	 * @param unknown_type $name
	 * @param unknown_type $id
	 * @param unknown_type $data['字段名']=值
	 */
	public function updateData($name,$id,$data){
		$col = array();
		foreach ($data as $key => $value){
			$col[] = $key."='".$value."'";
		}
		$sql = "UPDATE ".$name." SET ".implode(',', $col)." WHERE BID = ".$id;
		return $this->update($sql);
	}
	
}

?>